import com.sun.xml.internal.ws.api.model.wsdl.WSDLOutput;

import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: mac
 * Date: 2022-08-18
 * Time: 8:22
 */
public class RecursionDemo {
    //String不属于基本类型 - 属于引用类型

    //方法的递归：方法自己调用方法的过程
    //前提：
    //1.有一个人趋近于终止的条件
    //2.自己调用自己
    public  static  void func(int n){
        if (n == 1) return;
        func(n - 1);
    }
    //StackOverflowError -- 栈溢出错误
    public static void main0(String[] args) {
        func(3);
    }
    //"大杀器":递推公式 + 横向思考

    //1.求n的阶乘 n! = n * (n - 1)!
    public  static  int fac(int n){
        if (n == 1){
            return 1;
        }
        return n * fac(n - 1);
    }
    public static void main1(String[] args) {
        System.out.println(fac(5));
    }

    //2.求和
    public static int sumAdd(int n){
        if (n == 1){
            return 1;
        }
        return n + sumAdd(n - 1);
    }
    public static void main2(String[] args) {
        System.out.println(sumAdd(10));
    }

    //3.顺序打印数字
    public static void myprint(int n){
        if (n <= 9){
            System.out.print(n + " ");
            return;
        }
        myprint(n / 10);
        System.out.print(n % 10 + " ");
    }
    public static void main3(String[] args) {
        int n = 1234;
        myprint(n);
    }

    //4.数字之和
    public static int sumEvery(int n){
        if (n < 10){
            return n;
        }
        return n % 10 + sumEvery(n / 10);
    }
    public static void main4(String[] args) {
        System.out.println(sumEvery(1234));
    }

    //5.斐波那契数列
    public static int fib(int n){
        if (n == 1 || n == 2){
            return 1;
        }
        int f1 = 1;
        int f2 = 1;
        int f3 = 0;
        for (int i = 3; i <= n; i++) {
            f3 = f1 + f2;
            f1 = f2;
            f2 = f3;
        }
        return f3;
    }
    public static void main(String[] args) {
        System.out.println(3);
    }

    //6.青蛙跳台阶
    public static int frogjump(int n){
        if (n == 1 || n == 2){
            return n;
        }
        return frogjump(n - 1) + frogjump(n - 2);
    }
    public static void main6(String[] args) {
        Scanner scanner = new Scanner(System.in);
        while (scanner.hasNextInt()){
            int n = scanner.nextInt();
            System.out.println(frogjump(n));
        }
    }
}
